package com.example.common.response;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@ApiModel(value = "通用API接口返回", description = "Common Api Response")
public class R<T> implements Serializable {

    /**
     * 状态码 0 表示成功，1表示处理中，-1表示失败
     */
    //@ApiModelProperty(required = true, value = "返回状态码", dataType = "int", example = "0", position = 0)
    @ApiModelProperty(value = "返回状态码", required = true, position = 1)
    private Integer code;

    /**
     * 描述
     */
    //@ApiModelProperty(required = true, value = "返回信息", dataType = "string", example = "success", position = 1)
    @ApiModelProperty(value = "返回信息", required = true, position = 2)
    private String msg;

    /**
     * 数据
     */
    @ApiModelProperty(value = "返回状态码", required = true, position = 3)
    private T data;

    /**
     * 成功，传入数据
     */
    public static <T> R<T> OK() {
        return new R(0, "", "");
    }

    /**
     * 成功，传入数据
     */
    public static <T> R<T> OK(T data) {
        return new R<>(0, "", data);
    }

    /**
     * 成功，传入数据
     */
    public static <T> R<T> OK(Integer code, String msg, T data) {
        return new R<>(code, msg, data);
    }
    
    public static <T> R<T> ERROR(Integer code) {
        return new R(code, "", "");
    }

    /**
     * 失败，传入描述信息
     */
    public static <T> R<T> ERROR(String msg) {
        return new R(-1, msg, "");
    }

    /**
     * 失败，传入描述信息,状态码
     */
    public static <T> R<T> ERROR(Integer code, String msg) {
        return new R(code, msg, "");
    }
    
    public static <T> R<T> ERROR(Integer code, String msg, T data) {
        return new R<>(code, msg, data);
    }
}
